package com.android.client.presenter;

import java.io.Serializable;

import com.android.client.GreetingServiceAsync;
import com.android.client.event.GroupEditorEvent;
import com.android.client.handler.GroupEditorHandler;
import com.android.client.view.panel.GroupEditorPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class GroupSavePsenter extends AbstractPresenter {
	

	public GroupSavePsenter(Widget view, HandlerManager eventBus,
			GreetingServiceAsync service) {
		super(view, eventBus, service);
	}

	@Override
	protected void bind() {
		Button submit = eventBusSetting();
		submit.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				final GroupEditorPanel panel = (GroupEditorPanel) view;
				ListBox selector = panel.getSelector();
				Long groupId = new Long(selector.getValue(selector.getSelectedIndex()));
				GroupEditorEvent mEvent = new GroupEditorEvent(panel.getNumText().getText(), groupId);
				eventBus.fireEvent(mEvent);
			}
		});
	}

	private Button eventBusSetting() {
		final GroupEditorPanel panel = (GroupEditorPanel) view;
		eventBus.addHandler(GroupEditorEvent.TYPE, new GroupEditorHandler() {
			@Override
			public void saveGroupId(GroupEditorEvent event) {
				service.execute(event, new AsyncCallback<Serializable>(){
					@Override
					public void onFailure(Throwable caught) {
					}
					@Override
					public void onSuccess(Serializable result) {
					}
					
				});
			}
		});
		return panel.getSubmit();
	}
}
